<template>
    <div class="home-product">
    <HomePanel :title="cate.name" v-for="cate in titleData" :key="cate.id">
      <div class="box">
        <ul class="goods-list">
          <li @click="showDetail(good.id)" style="cursor: pointer;" class="goods-item"
          v-for="good in goodsData.filter((item) => item.category_id ==cate.id).slice(0, 100)" :key="good.id">
              <img :src="good.picture" alt="" />
              <p class="name ellipsis">{{ good.name }}</p>
              <p class="desc ellipsis">{{ good.description }}</p>
              <p class="price">&yen;{{ good.price }}</p>
          </li>
        </ul>
      </div>
    </HomePanel>
  </div>
  <el-dialog v-model="isShowDetail" width="800px" >
    <GoodsDetail ref="goodsForm" :currentid="currentid" />
   </el-dialog>
</template>
<script setup>
import { useRoute } from 'vue-router';
import { reactive,ref,onMounted,watch} from 'vue'
import { getCategoryList,getGoodsList} from '../../../api'
import HomePanel from './HomePanel.vue'
import GoodsDetail from './GoodsDetail.vue'
const route=useRoute();
const goodsData=ref([]);
const goodsForm = ref();
const isShowDetail = ref(false)
const currentid = reactive({
  id:"",
  isnew:false
})
const productId = ref(route.params.id);
const titleData = ref([]);
onMounted(() => {
    fetchData(productId.value);
});
const showDetail = (id) => {
  if (goodsForm.value) {
    goodsForm.value.resetForm(id);
  }
  currentid.id = id,
  isShowDetail.value = true;
}

const fetchData = async (id) => {
  const data = await getCategoryList();
  goodsData.value=await getGoodsList();
  titleData.value = data.filter((item) => item.pid ==productId.value).slice(0, 10);
};

watch(
  () => route.params.id,
  (newId) => {
    productId.value = newId;
    fetchData(newId);
  }
);

</script>

<style scoped lang='scss'>
.home-product {
  background: #fff;
  margin-top: 20px;
  .sub {
    margin-bottom: 2px;

    a {
      padding: 2px 12px;
      font-size: 16px;
      border-radius: 4px;

      &:hover {
        background: $xtxColor;
        color: #fff;
      }

      &:last-child {
        margin-right: 80px;
      }
    }
  }

  .box {
    display: flex;

    .cover {
      width: 240px;
      height: 610px;
      margin-right: 10px;
      position: relative;

      

      .label {
        width: 188px;
        height: 66px;
        display: flex;
        font-size: 18px;
        color: #fff;
        line-height: 66px;
        font-weight: normal;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translate3d(0, -50%, 0);

        span {
          text-align: center;

          &:first-child {
            width: 76px;
            background: rgba(0, 0, 0, 0.9);
          }

          &:last-child {
            flex: 1;
            background: rgba(0, 0, 0, 0.7);
          }
        }
      }
    }

    .goods-list {
      width: 990px;
      display: flex;
      flex-wrap: wrap;

      li {
        width: 240px;
        height: 300px;
        margin-right: 10px;
        margin-bottom: 10px;

        &:nth-last-child(-n + 4) {
          margin-bottom: 0;
        }

        &:nth-child(4n) {
          margin-right: 0;
        }
      }
    }

    .goods-item {
      display: block;
      width: 220px;
      padding: 20px 30px;
      text-align: center;
      transition: all .5s;

      &:hover {
        transform: translate3d(0, -3px, 0);
        box-shadow: 0 3px 8px rgb(0 0 0 / 20%);
      }

      img {
        width: 160px;
        height: 160px;
      }

      p {
        padding-top: 10px;
      }

      .name {
        font-size: 16px;
      }

      .desc {
        color: #999;
        height: 29px;
      }

      .price {
        color: $priceColor;
        font-size: 20px;
      }
    }
  }
}
.home-panel {
  background-color: #fff;

  .head {
    padding: 40px 0;
    display: flex;
    align-items: flex-end;

    h3 {
      flex: 1;
      font-size: 32px;
      font-weight: normal;
      margin-left: 6px;
      height: 35px;
      line-height: 35px;

      small {
        font-size: 16px;
        color: #999;
        margin-left: 20px;
      }
    }
  }
}
</style>